﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using XFControls.XFPanels.XFPanelItems;

namespace XFControls.XFPanels.XFPanelModals
{
    public class XFModalButton : IXFModelButton
    {
        public string Text { get; set; }
        public bool Selected { get; set; }
        public XFItemStyle Style { get; set; }

        private int _height;
        private int _width;

        public XFModalButton()
        {
            Style = new XFItemStyle()
            {
                TextFont = new Font(FontFamily.GenericSansSerif, 11, FontStyle.Bold),
                TextColor = Color.White,
                BackgroundColor = Color.RoyalBlue,
                SelectedBackgroundColor = Color.Black,
                Padding = 20
            };
        }

        public virtual XFModalClickResult GetClickResult() { throw new NotImplementedException(); }

        public void ItemPaint(System.Drawing.Graphics g, int x, int y)
        {
            var mes = g.MeasureString(Text, Style.TextFont);
            _width = (int)mes.Width + Style.PaddingLeft + Style.PaddingRight;
            _height = (int)mes.Height + Style.PaddingTop + Style.PaddingBottom;

            using (SolidBrush b = new SolidBrush(Selected ? Style.SelectedBackgroundColor : Style.BackgroundColor))
                g.FillRectangle(b, x, y, _width, _height);

            using (SolidBrush b = new SolidBrush(Style.TextColor))
                g.DrawString(Text, Style.TextFont, b, x + Style.PaddingLeft, y + Style.PaddingTop);
        }

        public int GetHeight()
        {
            if (_height > 0)
                return _height;

            using (Bitmap bm = new Bitmap(100, 100))
            {
                Graphics g = Graphics.FromImage(bm);

                var mes = g.MeasureString(Text, Style.TextFont);
                _width = (int)mes.Width + Style.PaddingLeft + Style.PaddingRight;
                _height = (int)mes.Height + Style.PaddingTop + Style.PaddingBottom;
            }

            return GetHeight();
        }

        public int GetWidth()
        {
            if (_width > 0)
                return _width;

            GetHeight();
            return GetWidth();
        }

        public void ResetHeight()
        {
            _height = 0;
        }

        public void Dispose()
        {
            if (Style != null)
                Style.Dispose();
        }
    }

    public class XFModalYesButton : XFModalButton
    {
        public XFModalYesButton()
            : base()
        {
            Text = "Yes";
        }

        public override XFModalClickResult GetClickResult()
        {
            return XFModalClickResult.Yes;
        }
    }

    public class XFModalNoButton : XFModalButton
    {
        public XFModalNoButton()
            : base()
        {
            Text = "No";
        }

        public override XFModalClickResult GetClickResult()
        {
            return XFModalClickResult.No;
        }

    }

    public class XFModalCancelButton : XFModalButton
    {
        public XFModalCancelButton()
            : base()
        {
            Text = "Cancel";
        }

        public override XFModalClickResult GetClickResult()
        {
            return XFModalClickResult.Cancel;
        }

    }
}
